package com.aishua.aishuawallet.activity;

import android.content.Intent;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.aishua.aishuawallet.Constant;
import com.aishua.aishuawallet.R;
import com.aishua.aishuawallet.bean.ResAuthBean;
import com.aishua.aishuawallet.diyview.LazyViewPager;
import com.aishua.aishuawallet.fregment.BaseFragment;
import com.aishua.aishuawallet.fregment.FourFregment;
import com.aishua.aishuawallet.fregment.OneFregment;
import com.aishua.aishuawallet.fregment.ThreeFregment;
import com.aishua.aishuawallet.fregment.TwoFregment;
import com.aishua.aishuawallet.utils.LogUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    private static final String TAG = "MainActivity-";
    private BottomNavigationView bottomNavigationView;
    private android.support.v7.widget.Toolbar toolbar;
    private LazyViewPager main_vp;
    private List<BaseFragment> fragmentslist;
    private ImageView img_changaccount;
    private ImageView img_code;
    private ImageView img_message;
    private ResAuthBean resAuthBean;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setTitle();
        getData();
        setBottomNavigationViewAndViewPager();
        initTitleClick();

    }

    private void getData() {

        resAuthBean = (ResAuthBean) getIntent().getSerializableExtra("resAuthBean");
        if (null != resAuthBean) {

           Constant.ADDRESS_OWN ="0x"+resAuthBean.getTokenInfo().get(0).getTokenAddr();
           Constant.TOKENID = resAuthBean.getTokenInfo().get(0).getTokenID();


        }
    }

    private void initTitleClick() {

        img_code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Constant.ISTOURIST){

                    touristMode();

                }else {

                    Intent intent = new Intent(MainActivity.this, ScannerActivity.class);
                    startActivityForResult(intent,1000);

                }
            }
        });

    }

    private void initView() {

        toolbar = findViewById(R.id.toolbar);
        main_vp = findViewById(R.id.main_vp);
        img_changaccount = findViewById(R.id.img_changaccount);
        img_code = findViewById(R.id.img_code);
        bottomNavigationView = findViewById(R.id.bottomNavigationView);
        img_message = findViewById(R.id.img_message);

    }

    private void setTitle() {

        toolbar.setTitle(getResources().getString(R.string.navg_wallet));

    }

   private int position =0;
    private void setBottomNavigationViewAndViewPager() {

        initFregmentData();
        main_vp.setAdapter(new MainPagetAdapter(getSupportFragmentManager()));
        // 为ViewPager添加页面改变事件
        main_vp.setOnPageChangeListener(new LazyViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                // 将当前的页面对应的底部标签设为选中状态
                bottomNavigationView.getMenu().getItem(position).setChecked(true);
                turnItem(position);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                switch (item.getItemId()) {
                    case R.id.navigation_home:
                        // 跳转到对应的页面
                        main_vp.setCurrentItem(0);
                        position =0;
                        break;
                    case R.id.navigation_dashboard:
                        main_vp.setCurrentItem(1);
                        position =1;
                        break;
                    case R.id.navigation_notifications:
                        main_vp.setCurrentItem(2);
                        position =2;

                        break;
                    case R.id.navigation_person:
                        main_vp.setCurrentItem(3);
                        position =3;
                        break;
                }
                turnItem(position);
                return true;
            }
        });


    }

    private void turnItem(int position) {
        if (position == 0) {

            toolbar.setTitle(getResources().getString(R.string.navg_wallet));            img_changaccount.setVisibility(View.VISIBLE);
            img_code.setVisibility(View.VISIBLE);
            img_message.setVisibility(View.GONE);


        } else if (position == 1) {

            toolbar.setTitle(getResources().getString(R.string.navg_manager));
            img_changaccount.setVisibility(View.GONE);
            img_code.setVisibility(View.GONE);
            img_message.setVisibility(View.GONE);

        } else if (position == 2) {

            toolbar.setTitle(getResources().getString(R.string.navg_find));
            img_changaccount.setVisibility(View.GONE);
            img_code.setVisibility(View.GONE);
            img_message.setVisibility(View.VISIBLE);

        } else {

            toolbar.setTitle(getResources().getString(R.string.navg_my));
            img_changaccount.setVisibility(View.INVISIBLE);
            img_code.setVisibility(View.INVISIBLE);
            img_message.setVisibility(View.VISIBLE);

        }
    }

    private void initFregmentData() {

        fragmentslist = new ArrayList<>();

        OneFregment oneFregment = new OneFregment();
        Bundle bundle = new Bundle();
        LogUtils.v(TAG+"resAuthBean:"+resAuthBean);
        bundle.putSerializable("resAuthBean", resAuthBean);
        oneFregment.setArguments(bundle);
        TwoFregment twoFregment = new TwoFregment();
        ThreeFregment threeFregment = new ThreeFregment();
        FourFregment fourFregment = new FourFregment();
        fragmentslist.add(oneFregment);
        fragmentslist.add(twoFregment);
        fragmentslist.add(threeFregment);
        fragmentslist.add(fourFregment);

    }

    public class MainPagetAdapter extends FragmentPagerAdapter {

        public MainPagetAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

            return fragmentslist.get(position);
        }

        @Override
        public int getCount() {

            return fragmentslist.size();
        }

    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);


//        oma方式
        if (resultCode ==1001){

            LogUtils.v(TAG+data.getStringExtra("result"));
            Intent intent = new Intent(this,TransferActivity.class);
            intent.putExtra("result",data.getStringExtra("result"));
            startActivity(intent);

        }

    }
}
